<template>
  <div class="resource">
    <el-card shadow="hover">
      <div style="margin-bottom: 10px;">
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <el-button
          style="margin-left: 20px;"
          @click="search"
          icon="el-icon-search"
          size="small"
          type="cyan"
          >搜索</el-button
        >
        <el-button
          @click="cancelData"
          icon="el-icon-circle-close"
          size="small"
          >重置</el-button
        >
      </div>
      <el-row :gutter="20">
        <el-form
          ref="params"
          :model="params"
          label-width="100px"
          size="small"
          class="searchForm"
        >
          <el-col :span="6">
            <el-form-item label="公司名称:" prop="companyName">
              <el-input
                v-model="params.companyName"
                size="small"
                clearable
                @change="search"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系人:" prop="linkman">
              <el-input
                v-model="params.linkman"
                size="small"
                @change="search"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="联系电话:" prop="phone">
              <el-input
                v-model="params.phone"
                size="small"
                clearable
                @change="search"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="审核状态:" prop="status">
              <el-select
                v-model="params.status"
                filterable
                placeholder="请选择"
                clearable
                @change="search"
              >
                <el-option
                  v-for="item in examineList"
                  :key="item.id"
                  :label="item.title"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6" >
            <el-form-item label="归属人选择" prop="belonger">
              <el-select
                v-model="params.belonger"
                filterable
                clearable
                placeholder="请选择归属人"
              >
                <el-option
                  v-for="item in examineList2"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="8">
            <el-form-item label="公司地址:" prop="address">
              <el-input
                v-model="params.address"
                size="small"
                clearable
                @change="search"
              ></el-input>
            </el-form-item>
          </el-col> -->
        </el-form>
      </el-row>
    </el-card>

    <div class="container">
      <div class="handle-box">
        <el-button
          size="small"
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
          class="mr10"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          class="handle-del mr10"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        >
      </div>

      <!-- <el-card shadow="hover">
        <h4 style="margin-bottom: 10px">咨询公司管理</h4> -->
        <el-table
          max-height="550"
          v-loading="loading"
          :data="tableData"
          border
          class="table"
          ref="multipleTable"
          header-cell-class-name="table-header"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="60"
            align="center"
          ></el-table-column>
          <el-table-column label="公司名称" min-width="260" align="center">
            <template slot-scope="scope">
              <span
                class="blue"
                style="text-decoration: underline; margin-left: 5px"
                @click="handleEdit(scope.$index, scope.row)"
                >{{ scope.row.companyName }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            prop="linkman"
            label="联系人姓名"
            align="center"
            min-width="150"
          ></el-table-column>
          <el-table-column
            prop="phone"
            label="联系方式"
            align="center"
            min-width="150"
          ></el-table-column>
          <el-table-column label="归属人" align="center" min-width="150">
            <template slot-scope="scope">
              <span>{{ status2(scope.row.belonger) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="公司状态" align="center" min-width="150">
            <template slot-scope="scope">
              <span>{{ status(scope.row.status) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="营业执照" align="center" width="140">
            <template slot-scope="scope">
              <viewer>
                <img style="height: 80px" :src="imgUrl + scope.row.businessLicense" />
              </viewer>
            </template>
          </el-table-column>
          <el-table-column label="公司地址" align="center" min-width="120" prop="address" :show-overflow-tooltip="true"></el-table-column>
          <!-- <el-table-column
            prop="person"
            label="业务员数量"
            align="center"
            min-width="150"
          ></el-table-column> -->
          <!-- <el-table-column label="订单数量" align="center" min-width="150"> -->
            <!-- <template slot-scope="scope" v-if="scope.row.order||scope.row.order>0">{{ scope.row.order }}</template> -->
            <!-- <template>暂无订单</template> -->
          <!-- </el-table-column> -->
          <el-table-column label="创建时间" align="center" min-width="160">
            <template slot-scope="scope">{{ formatT(scope.row.createTime) }}</template>
          </el-table-column>
          <el-table-column
            label="操作"
            min-width="230"
            align="center"
            fixed="right"
          >
            <template slot-scope="scope">
              <el-button
                type="success"
                size="mini"
                icon="el-icon-thumb"
                v-if="scope.row.status == 0"
                v-hasPermi="['system:company:check']"
                @click="handleExamine(scope.$index, scope.row)"
                >审核</el-button
              >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                @click="handleEdit(scope.$index, scope.row)"
                >编辑</el-button
              >
              <el-button
                type="danger"
                size="mini"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      <!-- </el-card> -->
      <div class="pagination">
        <pagination
          v-show="pageTotal > 0"
          :total="pageTotal"
          :page.sync="params.pageNum"
          :limit.sync="params.pageSize"
          @pagination="getData00"
        />
      </div>
    </div>
    <!-- 新增编辑 -->
    <el-dialog :title="title666" :visible.sync="dialogVisible" width="600px">
      <el-form
        :rules="rules2"
        ref="formData"
        :model="formData"
        label-width="120px"
        size="small"
        class="form-box"
      >
        <el-row :gutter="20">
          <el-col :span="20">
            <el-form-item label="公司名称" prop="companyName">
              <el-input
                v-model="formData.companyName"
                :disabled="editFlags"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="联系人" prop="linkman">
              <el-input v-model="formData.linkman"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="formData.phone"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18" >
            <el-form-item label="归属人选择" prop="belonger">
              <el-select
                v-model="formData.belonger"
                filterable
                placeholder="请选择归属人"
              >
                <el-option
                  v-for="item in examineList2"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="20">
            <el-form-item label="业务员数量">
              <el-input v-model="formData.address"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="20" v-if="orderNum_show">
            <el-form-item label="订单数量">
              <el-input v-model="formData.landlinePhone"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="20">
            <el-form-item label="营业执照" prop="businessLicense">
              <el-upload
                class="avatar-uploader"
                :action="upImage + '/upload/file'"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess"
              >
                <img
                  v-if="formData.businessLicense"
                  :src="imgUrl + formData.businessLicense"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="20">
            <el-form-item label="公司地址">
              <el-input v-model="formData.address"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="20">
            <el-form-item label="公司介绍">
              <el-input
                :rows="6"
                type="textarea"
                v-model="formData.desc"
              ></el-input>
            </el-form-item> -->
          <!-- </el-col> -->
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer" v-if="examine">
        <el-button @click="refuse" type="danger" size="small">拒绝</el-button>
        <el-button @click="agree" type="success" size="small">同意</el-button>
      </span>
      <span slot="footer" class="dialog-footer" v-else>
        <el-button @click="dialogVisible = false" size="small">取消</el-button>
        <el-button @click="submitForm()" type="primary" size="small"
          >确定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { checkPermi, checkRole } from "@/utils/permission"; // 权限判断函数
import {
  company_add,
  companyList,
  company_update,
  company_delete,
  company_sure,
  company_get
} from "@/api/zixun_company/zixun_company";
import { getToken } from "@/utils/auth";
import { listUser} from "@/api/system/user";
export default {
  components: {},
  props: [],
  data() {
    return {
      examineList2:[],
      // 非多个禁用
      multiple: true,
      //删除
      ids: [],
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      examineList: [
        {
          id: '0',
          title: "待审核",
        },
        {
          id: 1,
          title: "已关闭",
        },
        {
          id: 2,
          title: "审核通过",
        },
        {
          id: -1,
          title: "已拒绝",
        },
      ],
      orderNum_show: false,
      examine: false,
      title666: "",
      rules2: {
        phone: [{ required: true, message: "请输入联系电话", trigger: "blur" }],
        companyName: [
          { required: true, message: "请输入公司名称", trigger: "blur" },
        ],
        linkman: [
          { required: true, message: "请输入联系人名称", trigger: "blur" },
        ],
        businessLicense: [
          { required: true, message: "请上传图片", trigger: "blur" },
        ],
        address: [
          { required: true, message: "请输入公司地址", trigger: "blur" }
        ],
        belonger:[
          { required: true, message: "请选择归属人", trigger: "blur" }
        ]
      },
      input: "",
      openFlag: true,
      loading: true,
      //数据
      tableData: [],
      params: {
        pageNum: 1,
        pageSize: 10,
        status: null
      },
      pageTotal: 0,
      dialogVisible: false, // 是否展示编辑页面
      // 新增/编辑数据
      formData: {
        businessLicense: "",
      },
      customer: {
        customerId: "",
      },
      paramsFlag: 0,
      editFlags: false,
    };
  },
  computed: {},
  created() {
    this.getData00();
  },
  mounted() {
    this.listUser()
  },
  watch: {},
  methods: {
    checkRole,
    checkPermi,
    company_get(id){
      company_get(id).then(res=>{
        console.log(res);
        this.formData = res.data
      })
    },
    // 获取人员
    listUser() {
      this.loading = true;
      listUser(this.addDateRange({
        pageNum:1,
        pageSize:99999999
      }, {})).then(response => {
          this.examineList2 = response.rows;
        }
      );
    },
    // 状态编译
    status(val) {
      var str = this.examineList.find(function (item) {
        return item.id == val;
      });
      if (str) return str.title;
    },
    // 归属人编译
    status2(val) {
      var str = this.examineList2.find(function (item) {
        return item.userId == val;
      });
      if (str) return str.userName;
    },
    handleAvatarSuccess(res, file) {
      if (res.code == 200) {
        this.formData.businessLicense = res.url;
        console.log(this.formData.businessLicense);
        this.$message.success(`上传成功`);
      }
    },
    //   获取创建时间
    createTime(e) {
      console.log(e);
    },
    // 拒绝
    refuse() {
      this.formData.status = -1;
      this.$confirm("确定拒绝该请求吗？", "提示", {
        type: "error",
      })
        .then(() => {
          this.dialogVisible = false;
          this.$message.error(`您已拒绝该请求`);
        })
        .catch(() => {});
      this.$prompt("确定拒绝该请求吗,请输入理由？", "提示", {
        type: "warning",
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPlaceholder: "请输入拒绝理由",
      })
        .then(({ value }) => {
          console.log(value);
          if (value) {
            /**进行网络请求 */
            company_sure(this.formData).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.dialogVisible = false;
                this.$message.error(`您已拒绝该请求`);
                this.getData00();
              }
            });
          } else {
            this.$message.error(`拒绝理由不能为空`);
          }
        })
        .catch(() => {});
    },
    // 同意
    agree() {
      this.formData.status = 2;
      this.$confirm("确定同意该请求吗？", "提示", {
        type: "success",
      })
        .then(() => {
          company_sure(this.formData).then((res) => {
            console.log(res);
            if (res.code == 200) {
              this.dialogVisible = false;
              this.$message.success(`您已通过该请求`);
              this.getData00();
            }
          });
          this.dialogVisible = false;
          this.$message.success(`您已同意该请求`);
        })
        .catch(() => {});
    },
    //   业务员数量
    handlePerson(value) {
      console.log(value);
    },
    // 新增修改的方法
    submitForm() {
      this.$refs["formData"].validate((valid) => {
        if (valid) {
          if (this.title666 == "新增") {
            company_add(this.formData).then((res) => {
              console.log(res);
              if (res.code == 200) {
                this.$message.success(`新增成功`);
                this.dialogVisible = false;
                this.getData00();
              }
            });
          } else {
            company_update(this.formData).then((res) => {
              if (res.code == 200) {
                this.$message.success(`修改成功`);
                this.dialogVisible = false;
                this.uploading = false;
                this.getData00();
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 打开搜索
    openSearch() {
      this.openFlag = !this.openFlag;
    },
    // 重置搜索框
    cancelData() {
      this.resetForm("params");
      this.getData00();
    },
    handleAdd() {
      this.formData = {
        businessLicense: "",
      };
      this.dialogVisible = true;
      this.orderNum_show = false;
      this.examine = false;
      this.title666 = "新增";
    },
    handleEdit(index, row) {
      // this.formData = row;
      this.company_get(row.id)
      this.dialogVisible = true;
      this.orderNum_show = true;
      this.examine = false;
      this.title666 = "编辑";
    },
    handleExamine(index, row) {
      // this.formData = row;
      this.company_get(row.id)
      this.dialogVisible = true;
      this.orderNum_show = true;
      this.examine = true;
      this.title666 = "审核";
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    // 删除操作
    handleSelectionChange(val) {
      this.ids = val.map((item) => item.id);
      this.single = val.length != 1;
      this.multiple = !val.length;
    },
    // 批量删除
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return company_delete(id);
        })
        .then(() => {
          this.getData00();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },
    //获取数据
    getData00() {
      companyList(this.params).then((res) => {
        if (res.code == 200) {
          this.tableData = res.rows;
          this.pageTotal = res.total;
          this.loading = false;
        }
      });
    },
    // 触发搜索按钮
    search() {
      this.$set(this.params, "pageNum", 1);
      this.paramsFlag = 0;
      this.getData00();
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.params, "pageNum", val);
      this.getData00();
    },
  },
};
</script>

<style scoped>
.resource {
  padding: 30px;
}
.container {
  margin-top: 20px;
}
.handle-box {
  margin-bottom: 10px;
}

.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.yellow {
  color: #f57506;
}
.green {
  color: green;
  cursor: pointer;
}
.blue {
  color: #409eff;
  cursor: pointer;
  text-decoration: underline;
}
.mr10 {
  margin-right: 10px;
}

.searchForm .el-form-item--mini.el-form-item,
.searchForm .el-form-item--small.el-form-item {
  margin-bottom: 1px;
}
.compangImg {
  display: block;
  width: 280px;
  height: 200px;
}
.avatar-uploader .el-upload {
  width: 148px;
  height: 148px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
  border: 1px dashed #ccc;
}

.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
